const App = getApp();
const func = require('../../utils/func');


Page({

    /**
     * 页面的初始数据
     */
    data: {
        order: {},
        //1-待付款 2-付款成功 3-待收货 4-订单已完成 5-交易关闭
        status: 1,
        show: false,

        config: {},
        order_id: 0,
        isLoading: true,
        orderDetail: {},
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let _this = this;
        _this.data.order_id = options.order_id;
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        let _this = this;
        // 获取指定商家订单信息
        _this.getShopOrderFind();
    },

    // 下拉刷新
    onPullDownRefresh() {
        let _this = this;
        // 获取订单列表
        _this.getShopOrderFind();
        wx.stopPullDownRefresh();
    },

    // 获取商家客户信息详情
    getShopOrderFind() {
        let _this = this;
        let postData = {
            order_id: _this.data.order_id
        };
        App._requestApi(_this, App.globalData.config.shopOrderDetailUrl, postData, function (res) {
            _this.setData({
                isLoading: false,
                config: res.data.config,
                orderDetail: res.data.detail,
            });
        });

        // let _this = this;
        // App._requestApi(_this, App.globalData.config.shopOrderDetailUrl, {
        //     order_id
        // }, result => {
        //     // 富文本转码
        //     let virtual_delivery = result.data.order.virtual_delivery;
        //     // 富文本转码
        //     if (virtual_delivery) {
        //         wxParse.wxParse('virtual_delivery', 'html', virtual_delivery, _this, 0);
        //     }
        //     _this.setData(result.data);
        //     _this.setCountDown();
        // });
    },

    // 订单立即付款
    orderSubmitPay(e) {
        let _this = this;
        let payOrderID = _this.data.orderDetail.order_id;
        let payOrderCode = _this.data.orderDetail.order_code;
        if (true === _this._orderValidity(payOrderID)) {
            // 发起付款请求
            _this._orderDirectPay(payOrderID, payOrderCode);
        }
    },

    // 发起付款请求
    _orderDirectPay: function(payOrderID, payOrderCode) {
        let _this = this;
        let postData = {
            action: 'directPay',
            order_id: payOrderID,
            order_code: payOrderCode
        };
        App._requestPost(_this, App.globalData.config.shopOrderPayUrl, postData, function (res) {
            if (res.data.weChatPay.return_code == 'FAIL') {
                App.showError(res.data.weChatPay.return_msg);
                return false;
            }
            App.wxPayment({
                order_id: payOrderID,
                payment: res.data.weChatPay,
                success: res1 => {
                    // 支付后续处理
                    _this._orderPayHandle(payOrderID, payOrderCode);
                },
                fail: res1 => {
                    if (!res.msg.error) App.showError(res.msg.error);
                },
            });
        },
        function (res) {
            App.showError(res.msg);
        });
    },

    // 支付后续处理
    _orderPayHandle: function(payOrderID, payOrderCode) {
        if (payOrderID && payOrderCode) {
            let _this = this;
            let postData = {
                order_id: payOrderID,
                order_code: payOrderCode
            };
            App._requestPost(_this, App.globalData.config.shopOrderPayHandleUrl, postData, function (res) {
				App.showSuccess(res.msg, function() {
					wx.navigateTo({url: res.url});
				});
            },
            function (res) {
                App.showError(res.msg);
            });
        }
    },
    
    // 跳转界面
    jumpToView(e) {
        func.jumpToUrl(e)
    },

    // 订单商品维权
    orderService(e) {
        let _this = this;
        let orderID = e.currentTarget.dataset.order_id;
        let detailsID = e.currentTarget.dataset.details_id;
        let productID = e.currentTarget.dataset.product_id;
        if (true === _this._orderValidity(orderID)) {
            let postData = {
                use: 'service',
                order_id: orderID,
                action: 'goodsDetect',
                details_id: detailsID,
                product_id: productID,
            };
            App._requestApi(_this, App.globalData.config.shopOrderServiceActionUrl, postData, function (res) {
                let jumpUrl;
                if (parseInt(res.data.apply_service) === 1 && parseInt(res.data.service_id) !== 0) {
                    jumpUrl = './service/detail?service_id=' + res.data.service_id;
                } else {
                    jumpUrl = './service/apply?order_id=' + orderID + '&details_id=' + detailsID + '&product_id=' + productID
                }
                wx.navigateTo({url: jumpUrl});
            });
        }
    },

    // 订单取消
    orderCancel(e) {
        let _this = this;
        let orderID = _this.data.orderDetail.order_id;
        if (true === _this._orderValidity(orderID)) {
            wx.showModal({
                title: "友情提示",
                content: "确认要取消该订单吗？",
                success(o) {
                    if (o.confirm) {
                        let postData = {
                            action: 'cancel',
                            order_id: orderID
                        };
                        App._requestPost(_this, App.globalData.config.shopOrderActionUrl, postData, function (res) {
                            App.showSuccess(res.msg, function() {
                                _this.getShopOrderFind();
                            });
                        });
                    }
                }
            });
        }
    },

    // 订单提醒发货
    orderRemindSend(e) {
        let _this = this;
        let orderID = _this.data.orderDetail.order_id;
        if (true === _this._orderValidity(orderID)) {
            wx.showModal({
                title: "友情提示",
                content: "需要提醒管理员发货？",
                success(o) {
                    if (o.confirm) {
                        let postData = {
                            action: 'remind',
                            order_id: orderID
                        };
                        App._requestPost(_this, App.globalData.config.shopOrderActionUrl, postData, function (res) {
                            App.showSuccess(res.msg);
                        });
                    }
                }
            });
        }
    },

    // 订单确认收货
    orderConfirmReceipt(e) {
        let _this = this;
        let orderID = _this.data.orderDetail.order_id;
        if (true === _this._orderValidity(orderID)) {
            wx.showModal({
                title: "友情提示",
                content: "确定已收到货物了吗？",
                success(o) {
                    if (o.confirm) {
                        let postData = {
                            action: 'confirm',
                            order_id: orderID
                        };
                        App._requestPost(_this, App.globalData.config.shopOrderActionUrl, postData, function (res) {
                         
                            App.showSuccess(res.msg, function() {
                                wx.navigateBack({
                                    delta:1
                                })
                            });
                        });
                    }
                }
            });
        }
    },

    // 订单再次购买
    orderAgainBuy() {
        let _this = this;
        let orderID = _this.data.orderDetail.order_id;
        if (true === _this._orderValidity(orderID)) {
            let postData = {
                action: 'againBuy',
                order_id: orderID,
                goods_id: 0,
            };
            App._requestPost(_this, App.globalData.config.shopOrderActionUrl, postData, function (res) {
                if (1 === parseInt(res.data.abnormal)) {
                    App.showError(res.msg);
                } else if (2 === parseInt(res.data.abnormal)) {
                    _this.setData({
                        abnormalTitle: res.msg,
                        abnormalOrderID: orderID,
                        abnormalGoods: res.data['abnormalGoods'],
                        abnormalGoodsID: res.data['abnormalGoodsID'],
                    });
                    _this._abnormalTips();
                } else {
                    App.showSuccess(res.msg, function() {
                        wx.switchTab({url: '/pages/flow/index'});
                    });
                }
            });
        }
    },

    // 提示弹窗展示隐藏
    _abnormalTips() {
        let _this = this;
        _this.setData({
            abnormal: !_this.data.abnormal
        });
    },

    // 订单再次购买(剔除异常商品加入购物车)
    orderAddCart() {
        let _this = this;
        let orderID = _this.data.abnormalOrderID;
        let abnormalGoodsID = _this.data.abnormalGoodsID;
        if (true === _this._orderValidity(orderID)) {
            let postData = {
                action: 'againBuy',
                order_id: orderID,
                goods_id: JSON.stringify(abnormalGoodsID),
            };
            App._requestPost(_this, App.globalData.config.shopOrderActionUrl, postData, function (res) {
                App.showSuccess(res.msg, function() {
                    wx.switchTab({url: '/pages/flow/index'});
                });
            });
        }
    },

    // 订单ID效验
    _orderValidity: function(orderID) {
        if (parseInt(orderID) > 0) {
            return true;
        } else {
            App.showError('请选择操作订单');
            return false;
        }
    },

    // 获取支付时间倒计时2个小时
    setCountDown() {
        let _this = this
        var time = '2022-10-12';
        var repTime = time.replace(/-/g, '/'); //用正则主要是把“2019-05-20 00:00:00”转换成“2019/05/0 00:00:00”兼容ios
        var timeTamp = Date.parse(repTime);
        var time = Date.parse(new Date())

        var cha = time - timeTamp
        var totalSecond = ( 2 * 60 * 60 * 1000 - cha) / 1000;
        var interval = setInterval(function () {
            var second = totalSecond ;

            var day = Math.floor(second / 3600 / 24);

            var hr = Math.floor((second - day * 3600 * 24) / 3600);
            var hrStr = hr.toString();
            if (hrStr.length == 1) hrStr = '0' + hrStr;
            // 分钟位
            var min = Math.floor((second - day * 3600 * 24 - hr * 3600) / 60);
            var minStr = min.toString();
            if (minStr.length == 1) minStr = '0' + minStr;

            // 秒位
            var sec = second - day * 3600 * 24 - hr * 3600 - min * 60;
            var secStr = sec.toString();
            if (secStr.length == 1) secStr = '0' + secStr;

            this.setData({
                countDownHour: hrStr,
                countDownMinute: minStr,
                countDownSecond: secStr,
            });
            totalSecond--;
        }.bind(this), 1000);
    },

    addEvaluate() {
        wx.navigateTo({
          url: '/pages/order/refund/refund',
        })
    },

    // 复制订单号
    Copy(e){
        func.Copy(e)
    },
    
    
});